# -*- python -*-
# ex: set syntax=python:

# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# This is the buildmaster config file for the 'nacl' bot. It must
# be installed as 'master.cfg' in your buildmaster's base directory
# (although the filename can be changed with the --basedir option to
# 'mktap buildbot master').

# It has one job: define a dictionary named BuildmasterConfig. This
# dictionary has a variety of keys to control different aspects of the
# buildmaster. They are documented in docs/config.xhtml .

import posixpath
import re

from buildbot.changes.filter import ChangeFilter
from buildbot.scheduler import Scheduler
from buildbot.schedulers.basic import AnyBranchScheduler

from common import chromium_utils

from master import build_utils
from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import annotator_factory

import config
import master_site_config

ActiveMaster = master_site_config.NativeClientSDK

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

config.DatabaseSetup(c)

####### CHANGESOURCES

def ChromiumNativeClientChangeFilter(commit_json, branch):
  if 'tree_diff' not in commit_json:
    return False
  for diff_entry in commit_json['tree_diff']:
    path = diff_entry['new_path']
    if (path == 'DEPS' or
        path.startswith('native_client_sdk/') or
        path.startswith('ppapi/')):
      return True
  return False


multi_poller = gitiles_poller.GitilesPoller(
    repo_url=config.Master.git_server_url + '/chromium/src',
    category='multi',
    change_filter=ChromiumNativeClientChangeFilter)

release_poller = gitiles_poller.GitilesPoller(
    repo_url=config.Master.git_server_url + '/chromium/src',
    branches=[re.compile('refs/tags/*')],
    category='multirel',
    pollInterval=300,
    svn_branch=lambda _, b: b.rpartition('/')[2],
    comparator=multi_poller.comparator)

c['change_source'] = [
    multi_poller,
    release_poller,
]


####### BUILDERS

# buildbot/process/factory.py provides several BuildFactory classes you can
# start with, which implement build processes for common targets (GNU
# autoconf projects, CPAN perl modules, etc). The factory.BuildFactory is the
# base class, and is configured with a series of BuildSteps. When the build
# is run, the appropriate buildslave is told to execute each Step in turn.

# the first BuildStep is typically responsible for obtaining a copy of the
# sources. There are source-obtaining Steps in buildbot/process/step.py for
# CVS, SVN, and others.


# The identifier of the factory is the build configuration. If two factories
# are using the same build configuration, they should have the same identifier.

# BuilderTesters using a custom build configuration.
all_factories = []

branches = ['multi', 'multirel']
for branch in branches:
  factories = []

  if branch == 'multi':
    branch_ext = '-multi'
  elif branch == 'multirel':
    branch_ext = '-multirel'
  else:
    assert False

  m = annotator_factory.AnnotatorFactory().BaseFactory(
      recipe='client.nacl.sdk.recipe_autogen')

  # SDKs
  # Label each factory with a catergory code like:
  # '1trunk' indicating: category 1, branch trunk.
  code = str(branches.index(branch) + 1) + branch

  factories.append(['windows-sdk' + branch_ext, code, m])
  factories.append(['mac-sdk' + branch_ext, code, m])
  factories.append(['linux-sdk' + branch_ext, code, m])
  if branch == 'multi':
    factories.append(['linux-sdk-asan' + branch_ext, code, m])
  all_factories += factories

s_multi = Scheduler(
    name='nacl-multi',
    change_filter=ChangeFilter(category='multi'),
    treeStableTimer=0,
    builderNames=['windows-sdk-multi', 'mac-sdk-multi',
                  'linux-sdk-multi', 'linux-sdk-asan-multi'])

s_multirel = AnyBranchScheduler(
    name='nacl-multirel',
    change_filter=ChangeFilter(category='multirel'),
    treeStableTimer=0,
    builderNames=[
      'windows-sdk-multirel', 'mac-sdk-multirel', 'linux-sdk-multirel'])

c['schedulers'] = [
  s_multi,
  s_multirel,
]

def mergeRequests(req1, req2):
  if (req1.buildername.endswith('-multirel') or
      req2.buildername.endswith('-multirel')):
    return False
  return req1.source.canBeMergedWith(req2.source)

c['mergeRequests'] = mergeRequests

# Convert factories to a list of (factory_name, factory_object) pairs.
factories_bare = [(f[0], f[2]) for f in factories]
# Create mapping of builder name to list of steps.
builder_steps = build_utils.ExtractFactoriesSteps(factories_bare)
all_steps = build_utils.AllFactoriesSteps(factories_bare)





# ----------------------------------------------------------------------------
# BUILDER DEFINITIONS

# The 'builders' list defines the Builders. Each one is configured with a
# dictionary, using the following keys:
#  name (required): the name used to describe this bilder
#  slavename (required): which slave to use, must appear in c['slaves']
#  builddir (required): which subdirectory to run the builder in
#  factory (required): a BuildFactory to define how the build is run
#  periodicBuildTime (optional): if set, force a build every N seconds
#  category (optional): it is not used in the normal 'buildbot' meaning. It is
#                       used by gatekeeper to determine which steps it should
#                       look for to close the tree.
#

c['builders'] = []
slaves = slaves_list.SlavesList('slaves.cfg', 'NativeClientSDK')
for f in all_factories:
  if 'multirel' in f[0]:
    sbuild = 'sdk-rel'
  else:
    sbuild = 'sdk'
  c['builders'].append({
      'name': f[0],
      'slavenames': slaves.GetSlavesName(builder=f[0]),
      'builddir': f[0],
      'slavebuilddir': sbuild,
      'factory': f[2],
      'category': '%s|full' % f[1],
      # Don't enable auto_reboot for people testing locally.
      'auto_reboot': ActiveMaster.is_production_host,
  })


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)


####### STATUS TARGETS

# Buildbot master url:
# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster,
                             order_console_by_time=True,
                             tagComparator=multi_poller.comparator,
                             public_html='../master.chromium/public_html',
                             templates=['./templates',
                                        '../master.client.nacl/templates'])
